#!/usr/bin/env python
# -*- coding: utf-8 -*

import serial
import serial.tools.list_ports
from Tkinter import *
import ttk
import tkMessageBox
import os
import sys
from datetime import datetime

def calculate(*args):
    star_t = datetime.now()
    if main_com.get()!='' and board_com.get()!='' and main_com.get()!=board_com.get():
        tp = serial.Serial(main_com.get(),19200,timeout=0) #非等待模式
        rp = serial.Serial(board_com.get(),19200,timeout=0) #非等待模式
        tf = open('tdata.txt','w+b') #二进制打开文件（读写）
        rf = open('rdata.txt','w+b')
        while()
        for i in range(0, 255):
            tp.write(chr(i))
            tf.write(rp.read())
            rp.write(chr(i))
            rf.write(tp.read())
        #fdata=rf.read()

        tf_size=tf.tell() #文件长度
        rf_size= rf.tell() #文件长度
        tf.seek(0,0) #将光标设回起点，准备读取内容
        rf.seek(0,0)

        rdata = bytearray(rf.read())
        tdata = bytearray(tf.read())

        tf.close()
        rf.close()

        print tf_size
        t_err=0
        if tf_size!=0:
            for i in range(0,tf_size):
                if i%256 !=tdata[i]:
                    t_err += 1
            t_err_rate='发送误码率为{0:.2f}%\n'.format(float(t_err)/tf_size*100)
        else:
            t_err_rate='发送数为0\n'
            #print a/tf_size*100
        t_err=0
        if rf_size!=0:
            for i in range(0,rf_size):
                if i%256 !=tdata[i]:
                    t_err += 1
            r_err_rate='接收误码率为{0:.2f}%\n'.format(float(t_err)/tf_size*100)
        else:
            r_err_rate='接收数为0\n'


        tkMessageBox.showinfo("测试结果", '{0}{1}'.format(t_err_rate, r_err_rate))


    else:
        tkMessageBox.showinfo("串口号错误", "串口不能为空且不能一样")

root = Tk()
root.title("FRIT板测试软件")

mainframe = ttk.Frame(root, padding="10m")  ##
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

##feet = StringVar()
##meters = StringVar()

main_n = StringVar()
board_n = StringVar()
time_n=StringVar()

##feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
##feet_entry.grid(column=2, row=1, sticky=(W, E))

##ttk.Label(mainframe, textvariable=meters).grid(column=1, row=1, sticky=(W, E))
ttk.Button(mainframe, text="开始", command=calculate).grid(column=3, row=3, sticky=W)

ttk.Label(mainframe, text="测试平台串口号").grid(column=1, row=1, sticky=E)
ttk.Label(mainframe, text="板件串口号").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="测试时长（单位：秒）").grid(column=1, row=3, sticky=E)
##ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)

main_com = ttk.Combobox(mainframe, textvariable=main_n)
board_com= ttk.Combobox(mainframe, textvariable=board_n)
time_com= ttk.Combobox(mainframe, textvariable=time_n)

#board_com= ttk.Combobox(mainframe, textvariable=board_n)

port_list = list(serial.tools.list_ports.comports())
port_list1=[]
if len(port_list) <= 0:
    print "The Serial port can't find!"
else:
    for port in port_list:
        port_list1.append(port[0])

main_com["values"] = port_list1
board_com["values"] = port_list1
time_com["values"]=(60,120,180,240,300)
#port_list_0 =list(port_list[0])
#port_serial = port_list_0[0]

print port_list


main_com["state"] = "readonly"
board_com["state"] = "readonly"

main_com.grid(column=2, row=1, sticky=W)    # 设置其在界面中出现的位置  column代表列   row 代表行
board_com.grid(column=2, row=2, sticky=W)    # 设置其在界面中出现的位置  column代表列   row 代表行
time_com.grid(column=2,row=3,sticky=W)
##players.pack()
main_com.current(0)  #设定默认值
board_com.current(1)
time_com.current(0)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

##feet_entry.focus()


root.mainloop()




##if __name__ == '__main__':
##    root = Tkinter.Tk()
##    TkFileDialogExample(root).pack()
##    root.mainloop()




